package com.marktotop.controller.business;


import com.marktotop.entity.business.Health;
import com.marktotop.error.BusinessException;
import com.marktotop.response.ResultBean;
import com.marktotop.service.IHealthService;
import com.marktotop.vo.system.ActiveUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Api(tags = "业务模块-健康上报接口")
@RestController
@RequestMapping("/business/health")
public class HealthController {

    @Autowired
    private IHealthService healthService;


    /**
     * 当前用户健康上报
     * @param health
     * @return
     */
    @ApiOperation(value = "健康上报",notes = "用户健康上报")
    @RequiresPermissions({"health:report"})
    @RequestMapping(value = "/report",method = RequestMethod.POST)
    public ResultBean report(@Validated @RequestBody Health health) throws BusinessException {
        ActiveUserVO activeUser = (ActiveUserVO) SecurityUtils.getSubject().getPrincipal();
        health.setUserId(activeUser.getUser().getId());
        if (!healthService.report(health)){
            return ResultBean.error().message("健康上报信息失败！");
        }
        return ResultBean.ok().message("健康上报信息成功！");
    }


    /**
     * 签到记录
     * @return
     */
    @ApiOperation(value = "健康记录",notes = "用户健康上报历史记录")
    @RequestMapping(value = "/history",method = RequestMethod.GET)
    public ResultBean history(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize") Integer pageSize){
        ActiveUserVO activeUser= (ActiveUserVO) SecurityUtils.getSubject().getPrincipal();
        Long id = activeUser.getUser().getId();
        return healthService.history(id,pageNum,pageSize);
    }


    /**
     * 今日是否已报备
     * @return
     */
    @ApiOperation(value = "是否报备",notes = "今日是否已报备")
    @RequestMapping(value = "/isReport",method = RequestMethod.GET)
    public ResultBean isReport(){
        ActiveUserVO activeUser = (ActiveUserVO) SecurityUtils.getSubject().getPrincipal();
        Health report = healthService.isReported(activeUser.getUser().getId());
        return ResultBean.ok().data("report",report);
    }
}
